package com.quyunshuo.module.home.activity.search

import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.quyunshuo.androidbaseframemvvm.base.ktx.observeLiveData
import com.quyunshuo.androidbaseframemvvm.base.ktx.visible
import com.quyunshuo.androidbaseframemvvm.common.ext.click
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseActivity
import com.quyunshuo.androidbaseframemvvm.common.utils.ToastUtil
import com.quyunshuo.module.home.databinding.HomeActivitySearchBinding
import com.quyunshuo.module.home.fragment.video.SearchAdapter
import com.quyunshuo.androidbaseframemvvm.network.bean.ArticleInfo
import com.quyunshuo.androidbaseframemvvm.network.bean.ArticleList
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import dagger.hilt.android.AndroidEntryPoint

/**
 * 首页
 *
 * @author Qu Yunshuo
 * @since 5/22/21 2:26 PM
 */
@AndroidEntryPoint
class SearchActivity : BaseActivity<HomeActivitySearchBinding, SearchViewModel>(),
    OnRefreshLoadMoreListener {

    /**
     * 通过 viewModels() + Hilt 获取 ViewModel 实例
     */
    override val mViewModel by viewModels<SearchViewModel>()

    override fun createVB() = HomeActivitySearchBinding.inflate(layoutInflater)
    private var page = 0
    private lateinit var adapter: SearchAdapter
    private val dataList = mutableListOf<ArticleInfo>()
    private var isRefresh = true;//true是刷新 false是累加
    private val keyWord = ""

    override fun HomeActivitySearchBinding.initView() {
        
        btnSearch.click {
            val keyWord = etSearch.text.toString().trim()
            mViewModel.doSearch(page,keyWord)
        }
        recyclerView.layoutManager = LinearLayoutManager(this@SearchActivity.baseContext)
        adapter = SearchAdapter(dataList)
        recyclerView.adapter = adapter
        refreshLayout?.apply {
            setOnRefreshLoadMoreListener(this@SearchActivity)
        }
        ivBack.click {
            finish()
        }

    }

    override fun initObserve() {
        observeLiveData(mViewModel.data, ::processData)
    }

    private fun processData(articleList: ArticleList?) {
        val datas = articleList?.datas
        if (isRefresh) {
            dataList.clear()
            if (datas!!.isEmpty()){
                ToastUtil.showToast("数据为空")
            }
            // 添加模拟数据
            datas!!.forEach{dataList.add(it)}
            mBinding.refreshLayout.finishRefresh()
        } else {
            if (datas!!.isEmpty()){
                ToastUtil.showToast("数据已加载完毕")
            }
            datas!!.forEach{dataList.add(it)}
            mBinding.refreshLayout.finishLoadMore()

        }
        adapter.notifyDataSetChanged()
        mBinding.clSearchResult.visible()
    }


    override fun initRequestData() {
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        page = 1
        isRefresh = true
        mViewModel.doSearch(page,keyWord)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        page++
        isRefresh = false
        mViewModel.doSearch(page,keyWord)
    }

    override fun onDestroy() {
        mBinding.recyclerView.adapter = null
        super.onDestroy()
    }
}